﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Windows.Forms;

using libReadWrite;

using TTServer.Objects;
using TTServer.Messages;

namespace TTServer
{
    public class SocketServerThread: ServerThread
    {
        private Socket _connectedSocket = null;
        
        public SocketServerThread(Socket connectedSocket)
        {
            try
            {
                if (connectedSocket != null) //Kiểm tra giá trị của đối số truyền vào
                {
                    this._connectedSocket = connectedSocket;
                    this._bigEndianReader = new BigEndianReaderForWifi(connectedSocket);
                    this._bigEndianWriter = new BigEndianWriterForWifi(connectedSocket);
                    this._msgReader = new MessageReader(this._bigEndianReader);
                }
                else
                {
                    throw new Exception();   
                }
            }
            catch
            {
                throw new Exception("Invaild Argument");
            }
        }

        public override void Terminate()
        {
            try
            {
                if (_connectedSocket != null)
                {
                    if (_connectedSocket.Connected)
                        _connectedSocket.Shutdown(SocketShutdown.Both);
                    _connectedSocket.Close();
                    _connectedSocket = null;
                }

                base.Terminate();
            }
            catch (Exception ex) {
                throw ex;
            }
        }
    }
}
